package stream.filter;

import com.google.common.collect.Lists;
import stream.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author cyt
 * @create 2020-08-11 14:54
 */
public class Filter {
    public static void main(String[] args) {

        /**
         * filter就是对流中的元素逐一的检测 ， 过滤一遍， 并且会返回一个新的 stream 流
         */
        IntStream stream = IntStream.of(1, 2, 3, 56, 7);
        stream.filter(n -> n > 2).forEach(System.out::println);

        ArrayList<Person> list = new ArrayList<Person>();
        list.add(new Person("cc", 12));
        list.add(new Person("vv", 23));
        list.add(new Person("bb", 2));

        System.out.println(list.size());
        System.out.println(list.get(list.size() - 1));


        Stream<Person> personStream = list.stream();
        System.out.println("fff: "+ personStream.filter(p -> p.source == 10).count());

        List<String> collect = personStream
                .filter(p -> p.source > 10)
                .map(s -> s.name)
                .collect(Collectors.toList());

        for (String s : collect) {
            System.out.println(s);
        }
        System.out.println("\n");

        list.stream()
                .filter((item)-> item.source > 10)
                .map(Person::getName)
                .collect(Collectors.toList())
                .forEach(System.out::print);

    }
}

